/**
 * format date object to get date string as pattern
 */

var $js = (function(){
	function kajs(){}
	
	/**
	 * format date as pattern string
	 * @param date
	 * @param pattern
	 * @returns {String}date as pattern string
	 */
	kajs.dateFormat = function(date, pattern) {
		if(date == null){
			throw 'date is null';
		}
	    var temp = new String(pattern);
	    var tempYear = date.getFullYear();
	    var tempMonth = date.getMonth() + 1;
	    var tempDate = date.getDate();
	    var tempHour = date.getHours();
	    var tempMinute = date.getMinutes();
	    var tempSecond = date.getSeconds();
	    temp = temp.replace(/yyyy/g, tempYear)
	               .replace(/yy/g, (new String(tempYear)).slice(-2))
	               .replace(/MM+/g, parseInt(tempMonth) < 10 ? '0' + tempMonth : tempMonth)
	               .replace(/M/g, tempMonth)
	               .replace(/dd+/g, parseInt(tempDate) < 10 ? '0' + tempDate : tempDate)
	               .replace(/d/g, tempDate)
	               .replace(/hh+/g, parseInt(tempHour) < 10 ? '0' + tempHour : tempHour)
	               .replace(/h/g, tempHour)
	               .replace(/mm+/g, parseInt(tempMinute) < 10 ? '0' + tempMinute : tempMinute)
	               .replace(/m/g, tempMinute)
	               .replace(/ss+/g, parseInt(tempSecond) < 10 ? '0' + tempSecond : tempSecond)
	               .replace(/s/g, tempSecond);
	    return temp;
	};
	
	/**
	 * check input is taiwan date or not
	 * @param input
	 * @param callback
	 * @returns {Boolean}
	 */
	kajs.isTaiwanDate = isTaiwanDate;
	function isTaiwanDate(input, callback){
		var TAIWAN_DATE_FORMAT = /^([\d-]?\d{1,2})[-\\\/]?(\d{2})[-\\\/]?(\d{2})$/;
		var result = false;
		var myMatch = null;
		if(input == null 
			|| input.constructor.name != 'String'){
			result = false;
		}else{
			myMatch = input.match(TAIWAN_DATE_FORMAT);
			if(myMatch == null){
				result = false;
			}else{
				result = true;
			}
		}
		if(typeof(callback) !== 'undefined'){
			callback(result, myMatch);
		}
		return result;
	};
	
	/**
	 * get date from input of taiwan date
	 * @param input
	 * @returns {Date} 
	 */
	kajs.getDateFromTaiwanDate = getDateFromTaiwanDate;
	function getDateFromTaiwanDate(input){
		if(input == null || (typeof(input) != 'string')){
			throw 'type of input data is wrong:' +  typeof(input);
		}
		var myDate = null;
		isTaiwanDate(input, function(result, myMatch){
			if(result){
				myDate = new Date();
				myDate.setFullYear(parseInt(getWestYearFromTaiwanYear(parseInt(myMatch[1]))), 
								   parseInt(myMatch[2])-1,
								   parseInt(myMatch[3]));
			}else{
				//do nothing
			}
		});
		return myDate;
	};
	
	/**
	 * get the year in west format from taiwan year string 'tYear'
	 * @param tYear
	 * @returns
	 */
	kajs.getWestYearFromTaiwanYear = getWestYearFromTaiwanYear;
	function getWestYearFromTaiwanYear(tYear){
		if(tYear > 0){
			return 1911 + tYear;
		}else if(tYear == 0){
			throw 'year is illegal:0';
		}else{
			return 1911 + tYear + 1;
		}
	};
	
	
	/**
	 * trim input string to clean string
	 * @param input
	 * @returns
	 */
	function trim(input){
		if(input == null){
			return null;
		}
		return input.replace(/(^\s*)|(\s*$)/g, "");
	}
	kajs.trim = trim;
	
	/**
	 * check input string is null or empty string.
	 * @param input
	 * @returns {Boolean} if input string is null or empty string, return true.
	 * Else return false; 
	 */
	kajs.isEmpty = function(input){
		var temp = trim(input);
		if(temp == null || temp.length == 0){
			return true;
		}else{
			return false;
		}
	};
	
	/**
	 * check the test answer equals ans.
	 * @param test
	 * @param ans
	 * @param description
	 * @returns {Boolean}
	 */
	kajs.assertEqual = function(test, ans, description){
		if(test === ans){
			console.log("true");
			return true;
		}else{
			console.log('false => test:' + test + "\tans:" + ans + "\t" + description);
			return false;
		}
	};
	
	/**
	 * assert the test in callback will throw exception
	 * @param callback;
	 * return {Boolean}
	 */
	kajs.assertException = (function (){
		function assert(callback){
			try{
				callback();
				console.log('false => test:' + callback + ' didn\'t throws exception');
				return false;
			}catch(e){
				console.log("true");
				return true;
			}
		}
		return assert;
	})();
	
	return kajs;
})();

exports.$js = $js;
